//  Date:         	08/08/2018
//  task:         	water analysis
//  project:      	World Development

****************************************************************
* Program to Generate table with coeff from regression and t-test
*****************************************************************

capture program drop make_balance
	program define make_balance, eclass
	syntax, balancevar(varlist) treatvar(varname)
				   
	orth_out `balancevar', by(`treatvar') se compare test // orth_out is not installed in Stata by default
	
	matrix balance=r(matrix)
	local rows=(rowsof(balance))
	local cols=(colsof(balance))
	
	svmat balance	
	
	forvalues i=1/`cols'{
	if `i'!=`cols'{
		gen temp_mean`i'=.
		gen temp_sd`i'=.
		local all_names=""
		forvalues j=1/`rows'{
		
			qui replace temp_mean`i'=balance`i' if _n==(`j'*2)-1
			qui replace temp_sd`i'=balance`i' if _n==(`j'*2)
			local current_name: word `j' of `balancevar'
			local all_names="`all_names' `current_name'"
	}
			mkmat temp_mean`i', matrix(temp_mean) nomissing 
			mkmat temp_sd`i', matrix(temp_sd) nomissing 
			
			matrix mean=temp_mean'
			matrix sd=temp_sd'
	
			matrix colnames mean= `all_names' 
			matrix colnames sd= `all_names'
	
	ereturn matrix mean=mean 
	ereturn matrix sd=sd 
	
		eststo balance`i' 
	
	}
	else{
		mkmat balance`i', matrix(temp_mean) nomissing 
		mkmat balance`i', matrix(temp_sd) nomissing 
	
		matrix mean=temp_mean'
		matrix sd=temp_sd'
			
		matrix colnames mean= `all_names'   
		
		ereturn matrix mean=mean 
		ereturn matrix sd=sd   
		eststo balance`i' 
			}
	}
	drop balance* temp*
	end
	
clear
set more off

*****************************************
* Bringin CLUSTER data for balance test 
*****************************************
use ubridge_clusters.dta, clear

replace DIST_HC_ARUA=int(DIST_HC_ARUA/1000)

global covariates adult_pop age poverty_census lugbara_share HHI_Ethnicity polarization HHI_Religion literacy employed_share education secondary Nonagriculture_share DIST_HC_ARUA
su $covariates

*****************************************
* Table 1 in paper's Appendix 
*****************************************

	make_balance, balancevar($covariates) treatvar(treat)
	estimates restore balance1 
	
	summ treat if treat==0 
	estadd scalar obs r(N)

	estimates restore balance2

	summ treat if treat==1 
	estadd scalar obs r(N)

	estimates restore balance3

	summ treat  
	estadd scalar obs r(N)
	estimates restore balance4

*****************************************
* Bringin VILLAGE data for balance test 
*****************************************
use ubridge_villages.dta, clear

	replace Arua_dist=Arua_dist/1000
	lab var Arua_dist "Distance to Arua (km)" 

renvars *_sc,   subst(_sc )
drop age_lc1  
renvars *_lc1,  subst(_lc1 ) 	

ren sc_frac fractionalization
ren sc_pol polarization

global covariates adult_pop age poverty_census lugbara_share HHI_Ethnicity polarization HHI_Religion literacy employed_share education secondary Nonagriculture_share Arua_dist HC_YN PS_YN
su $covariates

foreach y in $covariates{
	reg `y' treat, cl(cluster_id)
}

****************************************************************
* TABLE 4 in Supplementary Information
****************************************************************

	make_balance, balancevar($covariates) treatvar(treat)
	estimates restore balance1 
	
	summ treat if treat==0 
	estadd scalar obs r(N)

	estimates restore balance2

	summ treat if treat==1 
	estadd scalar obs r(N)

	estimates restore balance3

	summ treat  
	estadd scalar obs r(N)
	estimates restore balance4

exit